from flask import jsonify
from flask_cors import cross_origin
from flask_restful import Resource

from database import Database


# Mock user database
class GetArticles(Resource):
    @cross_origin(origin='*', headers=['Content-Type', 'Authorization'])
    def get(self):
        db = Database()

        article_all = db.execute_query("SELECT * FROM article ")

        articles1 = []
        if article_all:

            for article in article_all:
                article = {
                    "articleId": article[0],
                    "type": article[1],
                    "publisher": article[2],
                    "title": article[3],
                    "subtitle": article[4],
                    "content": article[5],
                    "imageUrl": article[6],
                    "date": article[7].strftime("%Y-%m-%d %H:%M:%S")
                }
                articles1.append(article)
            return jsonify(articles1)
        else:
            return {"message": "server internal error"}, 401
